package me.samlss.lighter.util;

import ohos.agp.utils.TextTool;

import java.util.Collection;

/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public final class Preconditions {
    private Preconditions() {
        // Utility class.
    }

    /**
     * checkArgument
     * @param expression expression
     * @param message message
     * @throws IllegalArgumentException
     */
    public static void checkArgument(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 检查NotNull
     * @param arg arg
     * @param <T> T
     * @return T
     */
    public static <T> T checkNotNull(T arg) {
        return checkNotNull(arg, "Argument must not be null");
    }

    /**
     * checkNotNull
     * @param arg arg
     * @param message message
     * @param <T> T
     * @return T
     * @throws NullPointerException
     */
    public static <T> T checkNotNull(T arg, String message) {
        if (arg == null) {
            throw new NullPointerException(message);
        }
        return arg;
    }

    /**
     * checkNotEmpty
     * @param string string
     * @return string
     * @throws IllegalArgumentException
     */
    public static String checkNotEmpty(String string) {
        if (TextTool.isNullOrEmpty(string)) {
            throw new IllegalArgumentException("Must not be null or empty");
        }
        return string;
    }

    /**
     * checkNotEmpty
     * @param collection collection
     * @param <T> T
     * @param <Y> Y
     * @return T
     * @throws IllegalArgumentException
     */
    public static <T extends Collection<Y>, Y> T checkNotEmpty(T collection) {
        if (collection.isEmpty()) {
            throw new IllegalArgumentException("Must not be empty.");
        }
        return collection;
    }
}
